package com.cheyun.netsalev3.viewmodel.aftersale

import android.content.Intent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Transformations
import com.cheyun.netsalev3.bean.aftersale.CarOwnerData
import com.cheyun.netsalev3.repository.aftersale.CarOwnerRepository
import com.cheyun.netsalev3.viewmodel.BaseViewModel
import java.io.Serializable

class ChooseCarOwnerActivityViewModel(var carOwnerRepository: CarOwnerRepository) : BaseViewModel() {

    var listdata = carOwnerRepository.loadAndroidData()
    var networkState = Transformations.switchMap(listdata) {
        carOwnerRepository.networkState
    }

    fun onClickItem(view: View, data: CarOwnerData) {
        var content = view.context
        if (content is AppCompatActivity) {
            var intent = Intent()
            intent.putExtra("data", data as Serializable)
            content.setResult(101, intent)
            content.finish()
        }
    }

    fun initData(hashMap:HashMap<String, String>) {
        carOwnerRepository.setEanbleLoad(true)
        carOwnerRepository.changeMap(hashMap)
        carOwnerRepository.dataSourceFactory.sourceLiveData.value?.invalidate()
    }


    fun refresh(hashMap:HashMap<String, String>? = null) {
        if(hashMap != null) {
            carOwnerRepository.changeMap(hashMap)
        }
        carOwnerRepository.dataSourceFactory.sourceLiveData.value?.invalidate()
    }
}